﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace MyVocationSpace
{
    public partial class Connections : System.Web.UI.Page
    {
        public int CurrentPage = 0;
        public int totalPage;
        public int pageNo = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] != null)
            {
                if (Request.QueryString["IDUser"] != null)
                {
                    int IdUser = Convert.ToInt32(Request.QueryString["IDUser"]);
                    ViewState["IdUser"] = IdUser;
                    GetData(IdUser);
                    if (!IsPostBack)
                    {
                        PopulateConnetion(IdUser);
                    }
                }
                else
                {
                    GetData(Convert.ToInt32(Session["UserID"]));
                }
            }
            else
            {
                Response.Redirect("Home.aspx");
            }
        }

        public void GetData(int Iduser)
        {
            //int UserId = Convert.ToInt32(Session["UserID"]);
            string[] Detail = BAL.Users.GetUserDetails(Iduser);
            username.InnerHtml = Detail[1] + " " + Detail[2];
            //username.ID =Iduser.ToString();
            Occu.InnerHtml = Detail[0];
            city.InnerHtml = Detail[7];
            country.InnerHtml = Detail[16];
            industry.InnerHtml = Detail[18];           
            org.InnerHtml = Detail[14];

            lblName.Text = Detail[1];

            string img = Detail[19];
            if (img != "")
            {
                idImg.Src = "UsersPhoto/" + img;
            }
            else
            {
                idImg.Src = "UsersPhoto/icon_no_photo_60x60.png";
            }
        }

        public void PopulateConnetion(int IDUser)
        {
            DataSet ds = BAL.Users.GetConnetionDetailByUSer(IDUser);

            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView;

            int noOfItem = pgds.Count;
            pgds.AllowServerPaging = true;
            pgds.PageSize = 10;

            totalPage = pgds.PageCount;
            ViewState["TotalPage"] = totalPage;

            pgds.CurrentPageIndex = CurrentPage;
            ViewState["CurrentPage"] = CurrentPage;

            lnkNext.Visible = !pgds.IsLastPage;
            lnkPre.Visible = !pgds.IsLastPage;

            int currpageShow = Convert.ToInt32(ViewState["CurrentPage"]) + 1;
            Label1.Text = "Page " + currpageShow.ToString() + " of " + ViewState["TotalPage"].ToString();
            Label2.Text = noOfItem + " items found. Showing upto 10 items per page.";

            if (pgds != null)
            {
                dlUserConnection.DataSource = pgds.DataSource;
                dlUserConnection.DataBind();
            }
        }

        protected void lnkPre_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            CurrentPage += 1;
            pageNo = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo += 1;
            PopulateConnetion(Convert.ToInt32(ViewState["IdUser"]));
        }

        protected void lnkNext_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            CurrentPage -= 1;
            pageNo = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo -= 1;
            PopulateConnetion(Convert.ToInt32(ViewState["IdUser"]));
        }

        protected void dlUserConnection_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlImage Htmlimg = (HtmlImage)e.Item.FindControl("imgPerson");
                int IDUser = Convert.ToInt32(dlUserConnection.DataKeys[e.Item.ItemIndex]);
                string img = Htmlimg.Src;
                if (Htmlimg.Src == "")
                {
                    Htmlimg.Src = "UsersPhoto/icon_no_photo_60x60.png";
                }
                else
                {
                    Htmlimg.Src = "UsersPhoto/" + img;
                }

                LinkButton lnkConnectSend = (LinkButton)e.Item.FindControl("lnkConnectSend");
                HtmlInputHidden hiddenIDConnectionStatus = (HtmlInputHidden)e.Item.FindControl("hiddenIDConnectionStatus");
                HtmlGenericControl spanMYConnection = (HtmlGenericControl)e.Item.FindControl("spanMYConnection");
               // HtmlGenericControl spnsentMsgTick = (HtmlGenericControl)e.Item.FindControl("spnsentMsgTick");
                HtmlGenericControl spnsentMsg = (HtmlGenericControl)e.Item.FindControl("spnsentMsg");
                HtmlGenericControl idsuccesstick = (HtmlGenericControl)e.Item.FindControl("idsuccesstick");               
                HtmlGenericControl spanSuccessMsg = (HtmlGenericControl)e.Item.FindControl("spanSuccessMsg");

                if (hiddenIDConnectionStatus.Value == "1")
                {
                    lnkConnectSend.Visible = false;
                    spanMYConnection.Visible = false;

                    idsuccesstick.Visible = true;
                    spanSuccessMsg.InnerHtml = "<font style=\"color:green; font-size:12px; font-weight:bold;\">Connection has been sent</font>";
                }
                else if (hiddenIDConnectionStatus.Value == "2")
                {
                    lnkConnectSend.Visible = false;
                    spanMYConnection.Visible = false;

                    idsuccesstick.Visible = true;
                    spanSuccessMsg.InnerHtml = "<font style=\"color:green; font-size:12px; font-weight:bold;\">Connected</font>";
                }
                else if (hiddenIDConnectionStatus.Value == "" || hiddenIDConnectionStatus.Value == "NULL")
                {
                    lnkConnectSend.Visible = true;
                    spanMYConnection.Visible = true;
                }

                else
                {
                    lnkConnectSend.Visible = true;
                    spanMYConnection.Visible = true;
                }

                lnkConnectSend.OnClientClick = "SendConnectionReq(" + IDUser + ",'" + spnsentMsg.ClientID + "','" + spanMYConnection.ClientID + "','" + lnkConnectSend.ClientID + "');return false;";

            }
        }

        protected void lnkConnectSend_Click(object sender, EventArgs e)
        {
            //LinkButton lnk = (LinkButton)sender;
            //if (lnk != null)
            //{
            //    int id = Convert.ToInt32(lnk.CommandArgument);
            //    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "SendConnectionReq(" + id + ");", true);
            //}
        }
    }
}